package cn.org.xuanguang.kepler.hormos.rdb.dml;


import cn.org.xuanguang.kepler.hormos.orm.acl.IPolicyInterceptorChain;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.NativeDmlOperator;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.delete.DeleteOperator;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.delete.DeleteOperatorParameter;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.insert.InsertOperator;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.insert.InsertOperatorParameter;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.select.SelectOperator;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.select.SelectOperatorParameter;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.update.UpdateOperator;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.update.UpdateOperatorParameter;
import cn.org.xuanguang.kepler.hormos.rdb.executor.SyncSqlExecutor;

/**
 *
 */
public class DefaultDml implements Dml {


    // SyncSqlExecutor的实例，用于同步执行SQL命令
    private SyncSqlExecutor syncSqlExecutor;


    // 数据库的版本信息
    private String dailatype;

    // IPolicyInterceptorChain的实例，用于拦截器链
    IPolicyInterceptorChain policyInterceptorChain;

    @Override
    public NativeDmlOperator createNativeDmlOperator() {
        return NativeDmlOperator.of(syncSqlExecutor);
    }

    DefaultDml(SyncSqlExecutor syncSqlExecutor, String dailatype, IPolicyInterceptorChain policyInterceptorChain) {
        this.syncSqlExecutor = syncSqlExecutor;
        this.dailatype = dailatype;
        if (policyInterceptorChain != null) {
            this.policyInterceptorChain = policyInterceptorChain;
        } else {
            this.policyInterceptorChain = new IPolicyInterceptorChain<>();
        }

    }

    @Override
    public InsertOperator createInsertOperator() {
        // 加载拦截器
        policyInterceptorChain.setTarget(new InsertOperatorParameter());
        policyInterceptorChain.execute();
        return InsertOperator.of(syncSqlExecutor, dailatype, (InsertOperatorParameter) policyInterceptorChain.getTarget());
    }

    @Override
    public SelectOperator createSelectOperator() {
        // 加载拦截器
        policyInterceptorChain.setTarget(new SelectOperatorParameter());
        policyInterceptorChain.execute();
        return SelectOperator.of(syncSqlExecutor, dailatype, (SelectOperatorParameter) policyInterceptorChain.getTarget());
    }

    @Override
    public UpdateOperator createUpdateOperator() {
        // 加载拦截器
        policyInterceptorChain.setTarget(new UpdateOperatorParameter());
        policyInterceptorChain.execute();
        return UpdateOperator.of(syncSqlExecutor, dailatype, (UpdateOperatorParameter) policyInterceptorChain.getTarget());
    }

    public static DefaultDml of(SyncSqlExecutor syncSqlExecutor, String dailatype, IPolicyInterceptorChain policyInterceptorChain) {
        return new DefaultDml(syncSqlExecutor, dailatype, policyInterceptorChain);
    }

    @Override
    public DeleteOperator createDeleteOperator() {
        // 加载拦截器
        policyInterceptorChain.setTarget(new DeleteOperatorParameter());
        policyInterceptorChain.execute();

        return DeleteOperator.of(this.syncSqlExecutor, this.dailatype, (DeleteOperatorParameter) policyInterceptorChain.getTarget());
    }
}
